<?php
class Tasks_controller extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
        $this->load->model('task');
        $this->load->model('Comment_model');
	}
	
	public function index(){
		$this->load->view('tasks_list_view');
	}
	
	/**
	 * Displays task detail view
	 * @param $id - task id
	 */
   	public function detail_view($id = NULL){
   		$result = $this->task->get_task($id);
   		$data = $result[0];
   		$this->load->view('task_detail_view', $data);
   	}
	
   	/**
   	 * Displays tasks list view
   	 */
   	public function list_view(){
   		$this->load->view('tasks_list_view');
   	}

	function get(){
		$id = $_POST['id'];
		$result = NULL;
		
		if($id != NULL && $id != ''){
			$task = $this->task->get_task($id);
			$result = $task[0];
		}
		echo json_encode($result);
	}
   	
   	/**
   	 * Saves a task into the database
   	 * @see CRUD_interface::save()
   	 */
   	public function save(){
   		$id						= $this->input->post('id');
   		$project_id				= $this->session->userdata('current_project');
   		$sprint_id				= $this->input->post('sprint_id');
   		$assigned_to_id			= $this->input->post('assigned_to_id');
		$subject 				= $this->input->post('subject');
		$description 			= $this->input->post('description');
		$priority 				= $this->input->post('priority');
		$status 				= $this->input->post('status');
		$estimated_hours 		= $this->input->post('estimated_hours');
		$worked_hours 			= $this->input->post('worked_hours');
		$percentaje_completed 	= $this->input->post('percentaje_completed');
		
		if($id != NULL && $id != ''){
			$this->task->update_task($id, $project_id, $sprint_id, $assigned_to_id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
		}
		else{
			$insertId = $this->task->create_task($project_id, $sprint_id, $assigned_to_id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
		}
		
		redirect('/index.php/tasks_controller', 'refresh');
   	}
   	
   	/**
   	 * Removes a task from database
   	 * @param $id - task id
   	 * @see CRUD_interface::remove()
   	 */
   	public function remove($id = NULL){
   		$this->task->delete_task($id);
		redirect('/index.php/tasks_controller', 'refresh');
   	}
   	
   	
   	/**
   	 * Gets tasks list for jqgrid
   	 */
   	public function get_json_tasks_list() {
   		$page 			= $this->input->post('page');
		$limit 			= $this->input->post('rows');
		$order_by_field = $this->input->post('sidx');
		$order_by_type 	= $this->input->post('sord');
		
		if(!$order_by_field) $sidx =1;
		
		$count = $this->task->get_tasks_count();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit;
		
		$responce->page 	= $page;
		$responce->total 	= $total_pages;
		$responce->records 	= $count;
		
		$tasks = $this->task->get_tasks($order_by_field, $order_by_type, $start , $limit); 
		
		for($i=0; $i< sizeof($tasks); $i++) {
			$responce->rows[$i]['id'] = $tasks[$i]['id'];
			$responce->rows[$i]['cell'] = array($tasks[$i]['id'], 
												$tasks[$i]['subject'], 
												$tasks[$i]['priority'],
												$tasks[$i]['creation_date'],
												$this->getStatus($tasks[$i]['status']),
												$tasks[$i]['estimated_hours'], 
												$tasks[$i]['worked_hours'], 
												$tasks[$i]['percentaje_completed'], 'action');
		}
     	
		echo json_encode($responce);
   	}
   	
   	public function getStatus($status){
   		switch ($status) {
		    case 0:
		        return "open";
		        break;
		    case 1:
		        return "In Progress";
		        break;
		    case 2:
	      		  return "Resolved";
		        break;
   		}
   	}
   	
   	public function save_comment(){
   		$this->load->helper('date');
   		
   		$comment 		= $this->input->post('comment');
		$ref_id			= $this->input->post('ref_id');
		$ref_type		= 'task';
		$created_by_id	= $this->session->userdata('user_id');
		$created_date	= date('Y-m-d h:m:s', now());
		
		return $this->Comment_model->create_comment($comment, $ref_id, $ref_type, $created_by_id, $created_date);
   	}
   	
   	public function get_task_comments($task_id){
   		$comments = $this->Comment_model->get_task_comments($task_id);
   		return $comments;
   	}
   	
   public function get_task_comments_json(){
   		$task_id = $_POST['task_id'];
   		$comments = $this->get_task_comments($task_id);
   		echo json_encode($comments);
   	}
   	
}